﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DotConferenceSql;

namespace WebInterface
{
    public partial class InscriptionConference : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if( IsPostBack ==false)
                RemplirConference();

        }

        protected void RemplirConference()
        {
            using (var query = new SqlQuery())
            {
                var select = new SelectQueryElement();
                select.Add("ID_Conference");
                select.Add("Nom");
                
                query.Elements.Add(select);

                query.Elements.Add(new FromTableQueryElement("Conference"));

                var command = query.BuildSqlCommand();

                try
                {
                    var dReader = command.ExecuteReader();

                    ListConference.DataValueField = "ID_Conference";
                    ListConference.DataTextField = "Nom";

                    ListConference.DataSource = dReader;
                    ListConference.DataBind();

                    dReader.Close();
                }
                catch (Exception ex)
                {
                    Response.Write(ex.ToString());
                }
            }

            if(ListConference.Items.Count>0)
            {
                SetTextCout();
                
            }
        }

        protected void ListConference_SelectedItemChanged(object sender, EventArgs e)
        {
            SetTextCout();
        }

        protected void SetTextCout()
        {
            LblCout.Text = GetCoutConference(Convert.ToInt32(ListConference.SelectedValue)).ToString("0.00 $");
        }

        protected decimal GetCoutConference(int idConference)
        {

            decimal retour = 0;
            using (var query = new SqlQuery())
            {
                var select = new SelectQueryElement();
                select.Add("coutMinimal");
                

                query.Elements.Add(select);

                query.Elements.Add(new FromTableQueryElement("Conference"));

                query.Elements.Add(new CustomWhereStatement(string.Format("ID_Conference={0}", idConference)));

                var command = query.BuildSqlCommand();

                try
                {
                    retour = Convert.ToDecimal(command.ExecuteScalar());
                    
                }
                catch (Exception ex)
                {
                    Response.Write(ex.ToString());
                }
            }

            return retour;
        }

        protected void btn_inscrire(object sender, EventArgs e)
        {
            var nouvelleInscription = new DotConferenceCore.GestionParticipant.Inscription();
            
            nouvelleInscription.LeParticipant = MesVariablesSessions.Instance.unParticipant;
            nouvelleInscription.NoConference = Convert.ToInt32(ListConference.SelectedValue);
            nouvelleInscription.AjouterAuMontantDu(GetCoutConference(nouvelleInscription.NoConference));
            

            using (var query = new SqlQuery())
            {
                var insertElement = new InsertIntoQueryElement();
                insertElement.Table = "Inscription";

                insertElement.Add("ID_Participant", nouvelleInscription.LeParticipant.NumeroParticipant);
                insertElement.Add("ID_Conference",nouvelleInscription.NoConference);
                insertElement.Add("Montant", nouvelleInscription.MontantDu);
                

                query.Elements.Add(insertElement);

                var insertCommand = query.BuildSqlCommand();

                try
                {
                    insertCommand.ExecuteNonQuery();
                    Response.Redirect("~/Default.aspx");
                }
                catch (Exception ex)
                {
                    Response.Write(ex.ToString());
                }
            }
        }
    }
}
